package com.codeberry.yws2;

import java.io.*;
import java.net.*;
import java.util.*;

public class RequestArgs {
  private Map<String, String> args = new HashMap<String, String>();

  public void putArgsFromString(String argsString) {
    String[] argsStr = argsString.split("&");
    for (String arg : argsStr) {
      String[] entry = arg.split("=");
      String key = decode(entry[0]);
      String value = "";
      if (entry.length >= 2) {
        value = decode(entry[1]);
      }
      this.args.put(key, value);
    }
  }

  public String getArg(String name) {
    return args.get(name);
  }

  private static String decode(String encodedString) {
    try {
      return URLDecoder.decode(encodedString, "utf-8");
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException("should not happen", e);
    }
  }

  public String toString() {
    return args.toString();
  }
}
